# Theme - Theme represents currently used theme.
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import os
import clutter
import ConfigParser
from ConfigParser import ParsingError

class Theme:
    """
    Entertainer frontend GUI theme.

    Object from this class represents currently used theme. All colors and
    imagefile paths should be queried from the object of this class. Theme
    object is created when frontend is executed. Theme object reads it's
    content from a current theme that is set in preferences.conf.
    """
    
    def __init__(self, theme_path):
        """
        Create a new theme. Read all data from the disk and initialize object.
        @param theme_path: Absolute path of the theme
        """
        self.path = theme_path
        self.thumbnail = os.path.join(theme_path, "thumbnail.png")
        theme = ConfigParser.ConfigParser()
        try:
            theme.readfp(open(os.path.join(theme_path, "theme.conf")))
        except ParsingError:
            raise Exception("Syntax error in theme file.")
        except IOError:
            raise IOError("Couldn't read theme file.")
            
        try:
            # Get theme information
            self.name = theme.get("Theme", "name")
            self.comment = theme.get("Theme", "comment")
            self.author = theme.get("Theme", "author")
            self.licence = theme.get("Theme", "licence")
            self.copyright = theme.get("Theme", "copyright")
            
            # Get theme font
            self.font = theme.get("Theme", "font")
            if self.font == "":
                self.font = "Sans"
            
            # Get theme colors
            self.color = {}
            self.color['background'] = theme.get("Colors", "background").split(',')
            self.color['title'] = theme.get("Colors", "title").split(',')
            self.color['subtitle'] = theme.get("Colors", "subtitle").split(',')
            self.color['screentitle'] = theme.get("Colors", "screentitle").split(',')
            self.color['text'] = theme.get("Colors", "text").split(',')
            self.color['menuitem_active'] = theme.get("Colors", "menuitem_active").split(',')
            self.color['menuitem_inactive'] = theme.get("Colors", "menuitem_inactive").split(',')
            self.color['arrow_foreground'] = theme.get("Colors", "arrow_foreground").split(',')
            self.color['arrow_background'] = theme.get("Colors", "arrow_background").split(',')
        except:
            raise Exception("Invalid theme.conf file!")
            
        img_path = os.path.join(theme_path, "images")
        self.image = {}
        self.image['menu_overlay'] = os.path.join(img_path, "menu_overlay.png")
        self.image['selector'] = os.path.join(img_path, "selector.png")
        self.image['selector_glow'] = os.path.join(img_path, "selector_glow.png")
        self.image['menuitem_bg'] = os.path.join(img_path, "menuitem_bg.png")
        self.image['rss_icon'] = os.path.join(img_path, "rss_icon.png")
        self.image['warning_icon'] = os.path.join(img_path, "warning_icon.png")
        self.image['new'] = os.path.join(img_path, "new.png")
        self.image['star'] = os.path.join(img_path, "star.png")
        self.image['star2'] = os.path.join(img_path, "star2.png")
        self.image['logo'] = os.path.join(img_path, "logo.png")
        self.image['disc'] = os.path.join(img_path, "disc.png")
        self.image['native_aspect_ratio'] = os.path.join(img_path, "native_aspect_ratio.png")
        self.image['widescreen_aspect_ratio'] = os.path.join(img_path, "widescreen_aspect_ratio.png")
        self.image['zoom_aspect_ratio'] = os.path.join(img_path, "zoom_aspect_ratio.png")
        self.image['compromise_aspect_ratio'] = os.path.join(img_path, "compromise_aspect_ratio.png")
        self.image['default_album_art'] = os.path.join(img_path, "default_album_art.png")
        self.image['default_movie_art'] = os.path.join(img_path, "default_movie_art.png")
        
        # Menu icons
        self.image['menu-videos'] = os.path.join(img_path, "menu-videos.png")
        self.image['menu-music'] = os.path.join(img_path, "menu-music.png")
        self.image['menu-rss'] = os.path.join(img_path, "menu-rss.png")
        self.image['menu-photographs'] = os.path.join(img_path, "menu-photographs.png")
        self.image['menu-disc'] = os.path.join(img_path, "menu-disc.png")
        self.image['menu-settings'] = os.path.join(img_path, "menu-settings.png")
        
    def getName(self):
        """
        Get the name of the theme
        @return - Name of theme as string
        """
        return self.name
        
    def getComment(self):
        """
        Get comment of the theme
        @return - Comment of the theme as string
        """
        return self.comment
        
    def getAuthor(self):
        """
        Get the author of the theme
        @return - Author's name as string
        """
        return self.author
        
    def getLicence(self):
        """
        Get the licence of the theme
        @return - Licence information as string
        """
        return self.licence
        
    def getCopyright(self):
        """
        Get the copyright of the theme
        @return - Copyright information as string
        """
        return self.copyright
    
    def getFont(self):
        """
        Get name of the font
        @return - Name of the font as string
        """
        return self.font
        
    def getThumbnailURL(self):
        """
        Get URL to the theme's thumbnail image
        @return - Absolute path of the thumbnail as string
        """
        return self.thumbnail
        
    def getImage(self, element):
        """
        Get URL of the image for given element.
        @param element:
        @return - Absolute path of the image as string
        """
        if self.image.has_key(element) and os.path.exists(self.image[element]):
            return self.image[element]
        else:
            raise Exception("No image available for given element.")
        
    def getColor(self, element):
        """
        Return color for the element.
        @param element: Name of the screen element (string)
        @return - Color as integer list [r,g,b,a]
        """
        if self.color.has_key(element):
            try:
                color = self.color[element]
                return clutter.Color(int(color[0]),
                                     int(color[1]),
                                     int(color[2]),
                                     int(color[3]))
            except:
                return clutter.Color(255, 0, 0, 255) 
        else:
            # On error we return bright red.
            return clutter.Color(255, 0, 0, 255)